<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    use HasFactory;

    protected $table = 'rooms';

    protected $fillable = ['name', 'f_id'];

    protected $appends = ['sensor'];

    public function floor()
    {
        return $this->belongsTo(Floor::class, 'f_id');
    }

    /**
     * @return int 假数据
     */
    public function getSensorAttribute()
    {
        $data = $this->fakeData();
        return $data[$this->status];
    }

    /**
     * @return void 假数据
     */
    protected function fakeData()
    {
        $data[2] = [ // 消防报警
            '环境温度' => random_int(30, 50) . '℃',
            '环境湿度' => random_int(71, 100) . '%',
            '烟感状态' => '异常',
        ];
        $data[1] = [ // 环境告警的数据
            '环境温度' => random_int(16, 29) . '℃',
            '环境湿度' => random_int(30, 70) . '%',
            '烟感状态' => '正常',
        ];
        $data[0] = [ // 运行正常的数据
            '环境温度' => random_int(16, 29) . '℃',
            '环境湿度' => random_int(30, 70) . '%',
            '烟感状态' => '正常',
        ];
        return $data;
    }

    /**
     * @return void 假数据
     */
    protected function fakeData2()
    {
        $data = [
            '照明开关' => rand(0, 1) ? '开' : '关',
            '人体感应' => rand(0, 1) ? '有人' : '无人',
        ];
    }


}
